package com.ctshk.common.utils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import com.ctshk.common.exception.CtsException;


/**
 * 中汽巴士接口测试类
 */
public class RlgOrderUtil {
	protected static Logger logger = LoggerFactory.getLogger(RlgOrderUtil.class);

    public static void main(String[] args) throws Exception {
//        HashMap map = new HashMap();
//        RlgOrderUtil.refundTicket(map);
    	int i = (char)'A';
    	System.out.println(i);
    }





    /**
     * 登录
     * @throws Exception
     */
    public static void login() throws Exception{
        Map<String, Object> result = RglBusUtil.login("202001");

        Map root = (Map) result.get("ROOT");
        Map body = (Map) root.get("BODY");



//        System.out.println(root);
//        System.out.println(head);
        System.out.println(body);

    }






    public static List<String[]> getAllSiteList() throws Exception{
        //先查询城市接口
        try{
            Map<String, Object> result = RglBusUtil.getCityList("202003");
            Map root = (Map) result.get("ROOT");
            Map body = (Map) root.get("BODY");
            Map rsp = (Map)body.get("RSP2003");
            List<Map> cityList = (List<Map>)rsp.get("CityList");
            List<String> cityCodeList = cityList.stream().map(map -> (String)map.get("CityCode")).collect(Collectors.toList());
            List<String> cityTypeCodeList = cityList.stream().map(map -> (String)map.get("CityTypeCode")).collect(Collectors.toList());
            int sum = 0;
            List<String[]> shift = new ArrayList<>();
            for(int j=0;j<cityCodeList.size();j++) {
                for (int k=0; k < cityCodeList.size(); k++) {
                    if (cityTypeCodeList.get(j).equals(cityTypeCodeList.get(k))) {
                        continue;
                    } else {
                        //查询所有班次接口
                        Map<String, Object> paramMap = new HashMap();
                        paramMap.put("departureCityNo", cityCodeList.get(j));
                        paramMap.put("arrivalCityNo", cityCodeList.get(k));
                        List<String[]> siteList = getSiteList(paramMap);
                        Assist.add(shift, siteList);
                        sum++;
                    }
                }
            }
            System.out.println("接口调用次数:::"+sum);
            return shift;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }

    }

    /**
     * 查询所有站点
     * @throws Exception
     */
    public static  List<Map<String, Object>> getstopStaionList() throws Exception{
        Map<String, Object> result = RglBusUtil.getCityList("202003");
        Map root = (Map) result.get("ROOT");
        Map body = (Map) root.get("BODY");
        Map rsp = (Map)body.get("RSP2003");
        List<Map> cityList = (List<Map>)rsp.get("CityList");
        List<Map<String, Object>> list = new ArrayList<>();
        if(!CollectionUtils.isEmpty(cityList)){
            for(Map map : cityList){
            	;
                List<Map> stationList = new ArrayList<>();
                 Object object = map.get("StationList");
                if(object instanceof Map){
                    stationList.add((Map)object);
                }else if(object instanceof List){
                    stationList = (List)object;
                }
                
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("cityName", map.get("CityName"));
                resultMap.put("cityCode", map.get("CityCode"));
                resultMap.put("stationList", stationList);
                list.add(resultMap);
            }
        }

        return list;

    }


    /**
     * 根据城市查询所有车次
     * @throws Exception
     */
    public static List<String[]> getSiteList(Map<String,Object> paramMap) throws Exception{
    	List<String[]> resultList = new ArrayList<>();
    	
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        String departureDate = sdf2.format(new Date());
        paramMap.put("interfaceId", "202044");
        paramMap.put("departureDate", departureDate);

        Map<String, Object> result = RglBusUtil.getAllSiteList(paramMap);
        Map root = (Map) result.get("ROOT");
        Map body = (Map) root.get("BODY");
        if(!Objects.isNull(body)){
            Map rsp = (Map) body.get("RSP2044");
            String str = (String)rsp.get("BusScheduleInfo");
            String str1=str.substring(0, str.indexOf("注"));
            String str2=str.substring(str1.length()+1, str.length());
            
            String[] stopsArr = null;
            while (true) {
            	int firstIndex = 0;
            	int secondIndex = 0;
            	int rollIndex = 0;
            	stopsArr = new String[13];
            	for (int i = 0; i < 13; i++) {
            		firstIndex = str2.indexOf(BConst.QUOTATION, rollIndex);
            		rollIndex = firstIndex + 1;
            		secondIndex = str2.indexOf(BConst.QUOTATION, rollIndex);
            		rollIndex = secondIndex + 1;
            		
            		stopsArr[i] = str2.substring(firstIndex + 1, secondIndex);
				}
            	
            	resultList.add(stopsArr);
            	
            	if (rollIndex >= str2.length()) {
        			break;
        		}
            	
            	str2 = str2.substring(rollIndex);
			}
        }

        return resultList;
    }
    
    



    /**
     * 锁定座位
     * @throws Exception
     */
    public static Map<String, Object> lockseat(Map<String, Object> orderMap, LocalDate departureDate) {
    	Map<String, Object> resultMap = new HashMap<>();
        String date = DateUtil.fmtLocalDate(departureDate, "yyyyMMdd");

        orderMap.put("interfaceId", "202005");
        orderMap.put("departureDate", date);
        orderMap.put("currency", "1");

        Map<String, Object> result = RglBusUtil.placeOrderLockSeat(orderMap);
        Map root = (Map) result.get("ROOT");
        Map body = (Map) root.get("BODY");

        if(!Objects.isNull(body)){
            Map rsp = (Map) body.get("RSP2005");
            String orderNo = (String) rsp.get("OrderNo");
            if (Assist.isBlank(orderNo)) {
            	throw new CtsException("中汽锁位订单号不存在");
            }
            resultMap.put("orderNo", orderNo);
            
            List<String> ticketNumList = new ArrayList<>();
            resultMap.put("ticketNumList", ticketNumList);
            Object orderTicketInfo = rsp.get("OrderTicketInfo");
            if(orderTicketInfo instanceof List){
            	List<Map> itemList = (List<Map>) orderTicketInfo;
            	if (Assist.isNotEmpty(itemList)) {
            		for (Map map : itemList) {
            			ticketNumList.add((String) map.get("SeatNo"));
					}
            	}
            }else if(orderTicketInfo instanceof Map){
            	Map itemMap = (Map) orderTicketInfo;
            	if (itemMap != null) {
            		ticketNumList.add((String) itemMap.get("SeatNo"));
            	}
            }
        }
        return resultMap;

    }


    /**
     * 出票
     * @throws Exception
     */
    public static List<Map> outTicket(Map<String, Object> drawMap) {

        drawMap.put("interfaceId", "202006");
        drawMap.put("payType", "1");
        drawMap.put("sellWay", "0000");
        drawMap.put("currencyType", "1");

        Map<String, Object> result = RglBusUtil.drawerOrder(drawMap);
        Map root = (Map) result.get("ROOT");
        Map body = (Map) root.get("BODY");
        
        if(!Objects.isNull(body)){
            Map rsp = (Map) body.get("RSP2006");
            List<Map> mapList = new ArrayList<>();
            Object object = rsp.get("PrintTicketInfo");
            if(object instanceof List){
                mapList = (List)object;
            }else if(object instanceof Map){
                mapList.add((Map)object);
            }
            
            if (Assist.isBlank(mapList)) {
            	throw new CtsException("中汽出票巴士票列表不存在");
            }
            return mapList;
        }
        return null;
    }

    /**
     * 已售车票查询
     */
    public static void querySaledTicket() throws Exception{

        Map<String, Object> queryOrderMap = new HashMap<>();
        queryOrderMap.put("interfaceId", "202007");
        queryOrderMap.put("ticketNo", "21030400002-1");
        queryOrderMap.put("orderNo", "S10304660002");
        queryOrderMap.put("isGetOrder", "0");

        Map<String, Object> result6 = RglBusUtil.querySaledOrder(queryOrderMap);
        Map root6 = (Map) result6.get("ROOT");
        Map head6 = (Map) root6.get("HEAD");
        Map body6 = (Map) root6.get("BODY");

        System.out.println("root6:" + root6);
        System.out.println("head6:" + head6);

        System.out.println("body6:" + body6);

    }


    /**
     * 退票
     */
    public static Map<String,String> refundTicket(Map<String, Object> refundOrderMap) throws Exception{

        refundOrderMap.put("interfaceId", "202008");
        refundOrderMap.put("confirmFlag", "0");
        refundOrderMap.put("forceReturnFlag", "1");
        Map<String, Object> result = RglBusUtil.refundOrder(refundOrderMap);
        Map root = (Map) result.get("ROOT");
        Map body = (Map) root.get("BODY");
        System.out.println("body:" + body);
        return body;
    }

    /**
     * 改签
     */
    public static void endorseTicket() throws Exception{

        Map<String, Object> endorseMap = new HashMap<>();
        endorseMap.put("interfaceId", "202009");
        endorseMap.put("originalTicketNo", "21030400002-1");
        endorseMap.put("newBusNo", "GZ0740-DIS1000");
        endorseMap.put("newDepartureDate", "20210305");


        Map<String, Object> result8 = RglBusUtil.endorseOrder(endorseMap);
        Map root8 = (Map) result8.get("ROOT");
        Map head8 = (Map) root8.get("HEAD");
        Map body8 = (Map) root8.get("BODY");

        System.out.println("root8:" + root8);
        System.out.println("head8:" + head8);

        System.out.println("body8:" + body8);

    }


    /**
     * 取消订单
     */
    public static void cancelTicket() throws Exception{

        Map<String, Object> cancelMap = new HashMap<>();
        cancelMap.put("interfaceId", "202011");
        cancelMap.put("orderNo", "S10304660002");


        Map<String, Object> result9 = RglBusUtil.cancelOrder(cancelMap);
        Map root9 = (Map) result9.get("ROOT");
        Map head9 = (Map) root9.get("HEAD");
        Map body9 = (Map) root9.get("BODY");

        System.out.println("root9:" + root9);
        System.out.println("head9:" + head9);

        System.out.println("body9:" + body9);

    }

}
